package com.wanmait.schedule.controller;

import com.wanmait.schedule.entity.Classes;
import com.wanmait.schedule.entity.Role;
import com.wanmait.schedule.entity.Schedule;
import com.wanmait.schedule.service.ClassesService;
import com.wanmait.schedule.service.ScheduleService;
import com.wanmait.schedule.util.AjaxResult;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/manage/schedule")
@RequiresPermissions("manage:schedule")//没有权限会抛出异常
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;
    @Resource
    private ClassesService classesService;

    @GetMapping("list")
    public AjaxResult list(){
        return AjaxResult.success(scheduleService.findAll());
    }

    @PostMapping("weeklist")
    public AjaxResult weeklist(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        Date date2 = new Date(date.getTime()+7*24*60*60*1000);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Classes> classesList = classesService.list();
        boolean flag=true;
        for(Classes classes:classesList){
            List<Schedule> schedules = scheduleService.findByClassesAndTime(classes.getId(),dateFormat.format(date),dateFormat.format(date2));
            Schedule[] scheduleList = new Schedule[7];
            Date d = date;
            for(int i=0;i<7;i++){
                for(Schedule schedule:schedules){
                    flag=false;
                    if(schedule.getCourseTime().toString().substring(0,10).equals(dateFormat.format(d))){
                        scheduleList[i]=schedule;
                    }
                }
                d = new Date(d.getTime()+24*60*60*1000);
            }
            classes.setScheduleList(scheduleList);
        }
        if(flag){
            return AjaxResult.error(20001,"无课表");
        }
        return AjaxResult.success(classesList);
    }

    @PostMapping("save")
    public AjaxResult save(@RequestBody Schedule schedule){
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //Date d = dateFormat.format(schedule.getCourseTime());
        //schedule.setCourseTime(date);
        System.out.println(schedule.getCourseTime()+"***********");
        if(schedule.getId()==null){
            schedule.setInputId(1);
            scheduleService.save(schedule);
        }
        else{
            scheduleService.updateById(schedule);
        }
        return AjaxResult.success("保存成功",schedule);
    }

    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        return AjaxResult.success(scheduleService.removeById(id));
    }

    @PostMapping("publish")
    public AjaxResult publish(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        Date date2 = new Date(date.getTime()+7*24*60*60*1000);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //查看上周课表是否发布
        List<Schedule> s = scheduleService.checkPublish(dateFormat.format(date));
        if(s.size()!=0){
            return AjaxResult.error(20001,"上周未发布");
        }
        scheduleService.publish(dateFormat.format(date),dateFormat.format(date2));
        return AjaxResult.success("发布成功");
    }

}
